<!DOCTYPE html>
<!-- saved from url=(0053)https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/ -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="description" content="CS 61A: Structure and Interpretation of Computer Programs">
    <meta name="keywords" content="CS 61A, Computer Science, CS, 61A, Programming, John DeNero, Berkeley, EECS">
    <meta name="author" content="Hany Farid, John DeNero">
    
    <meta name="viewport" content="width=device-width">

    <script src="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/jquery.min.js.下载"></script>
    <script src="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/bootstrap.min.js.下载"></script>
    <script src="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/hl.js.下载"></script>
    <script>
        // we aren't registering builtins since
        //      (1) they don't render differently in the current CSS from other names
        //      (2) it's a mess to list all of them. You can extract from the site but that takes effort
        // if (1) ceases to be true, (2) might be worth the effort. For now, we're leaving as is
        hljsRegister({
            'keyword': "define if cond and or let begin lambda mu quote delay cons-stream set! quasiquote unquote unquote-splicing define-macro"
        });
        hljs.initHighlightingOnLoad();
    </script>
    <script src="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/dark-mode.js.下载"></script>
    <link rel="stylesheet" href="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/bootstrap.min.css">
    <link rel="stylesheet" href="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/css">
    <link rel="stylesheet" href="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/style.css">
    <link rel="stylesheet" href="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/mono-blue.css">
    <link rel="icon" href="https://inst.eecs.berkeley.edu/~cs61a/fa20/assets/images/favicon.ico">

    

    <title>
Lab 2: Higher-Order Functions, Lambda Expressions | CS 61A Fall 2020
</title>
  </head>

  <body id="index" class="home vsc-initialized">
    <nav class="navbar navbar-default navbar-static-top">
      <div class="container noselect">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse-section">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="https://inst.eecs.berkeley.edu/~cs61a/fa20/">
            <img style="max-width:60px; margin-top: -20px;" class="logo" src="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/logo.png">
          </a>
        <button type="button" class="btn btn-primary" style="float: left; margin-top: 8px;"><span style="filter: grayscale(100%);">✏️</span></button></div>

        <div class="collapse navbar-collapse" id="navbar-collapse-section">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/weekly.html">Weekly Schedule</a></li>
            <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/office-hours.html">Office Hours</a></li>
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Staff
              </a>
              <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/instructor.html">Instructors</a></li>
                <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/TAs.html">TAs</a></li>
                <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/tutors.html">Tutors</a></li>
                <!-- <li><a href="/~cs61a/fa20/academic-interns.html">Academic Interns</a></li> -->
              </ul>
            </li>
            <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/resources.html">Resources</a></li>
            <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/articles/about.html">Syllabus</a></li>
            <li><a href="https://piazza.com/berkeley/fall2020/cs61a" target="_blank">Piazza</a></li>
            <li><a href="https://denero.org/feedback.html" target="_blank">Feedback</a></li>
<!--             <li><a href="https://edge.edx.org/courses/course-v1:BerkeleyX+CSW61A+2020_SP">EdX</a></li> -->
            <!-- <li><a href="https://scheme.cs61a.org">Scheme</a></li> -->
            <!-- <li><a href="https://sql.cs61a.org">SQL</a></li> -->
            <li><a href="https://code.cs61a.org/">Code</a></li>
            <li><a href="http://tutor.cs61a.org/">Tutor</a></li>
          </ul>
        </div>
      </div>
    </nav>

    <main id="content" class="container">
      
<div class="row">
  <div class="col-md-9">
    <header>
      <h1>
        
Lab 2: Higher-Order Functions, Lambda Expressions

        
        
        <ul class="inline-block list-inline">
          <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/lab02.zip" class="label label-outline">lab02.zip</a></li>
        </ul>
        
        
      </h1>
    </header>
    

<p><i>Due by 11:59pm on Tuesday, September 8.</i></p>




<h2 id="starter-files">Starter Files</h2>

<p>Download <a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/lab02.zip">lab02.zip</a>.
Inside the archive, you will find starter files for the questions in this lab,
  along with a copy of the <a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/ok">Ok</a> autograder.</p>






    
<h1 id="topics">Topics</h1>


<p>Consult this section if you need a refresher on the material for this lab. It's
okay to skip directly to <a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#required-questions">the questions</a> and refer back
here should you get stuck.</p>


        <button id="toggle-1" class="btn btn-outline btn-lg alwaystoggle toggle" style="display: inline-block;">
        Lambda Expressions<noscript> (enable JavaScript)</noscript>
        </button>
        <div class="solution toggle-1">

<h2 id="lambda-expressions">Lambda Expressions</h2>


<p>Lambda expressions are expressions that evaluate to functions by specifying two
things: the parameters and a return expression.</p>

<pre><code class="hljs python"><span class="hljs-keyword">lambda</span> &lt;parameters&gt;: &lt;<span class="hljs-keyword">return</span> expression&gt;</code></pre>

<p>While both <code>lambda</code> expressions and <code>def</code> statements create function objects,
there are some notable differences. <code>lambda</code> expressions work like other
expressions; much like a mathematical expression just evaluates to a number and
does not alter the current environment, a <code>lambda</code> expression
evaluates to a function without changing the current environment. Let's take a
closer look.</p>

<div class="table-responsive">
  <table class="table table-bordered">
      <tbody><tr>
          <th></th>
          <th>lambda</th>
          <th>def</th>
      </tr>
      <tr>
          <td>Type</td>
          <td><i>Expression</i> that evaluates to a value</td>
          <td><i>Statement</i> that alters the environment</td>
      </tr>
      <tr>
          <td>Result of execution</td>
          <td>Creates an anonymous lambda function with no intrinsic name.</td>
          <td>Creates a function with an intrinsic name and binds it to that
          name in the current environment.</td>
      </tr>
      <tr>
          <td>Effect on the environment</td>
          <td>Evaluating a <code>lambda</code> expression does <i>not</i> create or
          modify any variables.</td>
          <td>Executing a <code>def</code> statement both creates a new function
          object <i>and</i> binds it to a name in the current environment.</td>
      </tr>
      <tr>
          <td>Usage</td>
          <td>A <code>lambda</code> expression can be used anywhere that
          expects an expression, such as in an assignment statement or as the
          operator or operand to a call expression.</td>
          <td>After executing a <code>def</code> statement, the created
          function is bound to a name. You should use this name to refer to the
          function anywhere that expects an expression.</td>
      </tr>
      <tr>
          <td>Example</td>
          <td>
<pre><code class="hljs python"><span class="hljs-comment"># A lambda expression by itself does not alter</span>
<span class="hljs-comment"># the environment</span>
<span class="hljs-keyword">lambda</span> x: x * x

<span class="hljs-comment"># We can assign lambda functions to a name</span>
<span class="hljs-comment"># with an assignment statement</span>
square = <span class="hljs-keyword">lambda</span> x: x * x
square(<span class="hljs-number">3</span>)

<span class="hljs-comment"># Lambda expressions can be used as an operator</span>
<span class="hljs-comment"># or operand</span>
negate = <span class="hljs-keyword">lambda</span> f, x: -f(x)
negate(<span class="hljs-keyword">lambda</span> x: x * x, <span class="hljs-number">3</span>)</code></pre></td>
          <td><pre><code class="hljs python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">square</span><span class="hljs-params">(x)</span>:</span>
    <span class="hljs-keyword">return</span> x * x

<span class="hljs-comment"># A function created by a def statement</span>
<span class="hljs-comment"># can be referred to by its intrinsic name</span>
square(<span class="hljs-number">3</span>)</code></pre></td>
      </tr>
  </tbody></table>
</div>


            <iframe width="560" height="315" src="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/vCeNq_P3akI.html" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe>
</div>

<br>


        <button id="toggle-2" class="btn btn-outline btn-lg alwaystoggle toggle" style="display: inline-block;">
        Environment Diagrams<noscript> (enable JavaScript)</noscript>
        </button>
        <div class="solution toggle-2">

<h2 id="environment-diagrams">Environment Diagrams</h2>


<p>Environment diagrams are one of the best learning tools for understanding
<code>lambda</code> expressions and higher order functions because you're able to keep
track of all the different names, function objects, and arguments to functions.
We highly recommend drawing environment diagrams or using <a href="https://tutor.cs61a.org/">Python
tutor</a> if you get stuck doing the WWPD problems below.
For examples of what environment diagrams should look like, try running some
code in Python tutor. Here are the rules:</p>


<h3 id="assignment-statements">Assignment Statements</h3>


<ol>
  <li>Evaluate the expression on the right hand side of the <code>=</code> sign.</li>
  <li>If the name found on the left hand side of the <code>=</code> doesn't already exist in
  the current frame, write it in. If it does, erase the current binding. Bind the
  <em>value</em> obtained in step 1 to this name.</li>
</ol>

<p>If there is more than one name/expression in the statement, evaluate all the
expressions first from left to right before making any bindings.</p>

<iframe width="800" height="270" frameborder="0" src="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/iframe-embed.html">
</iframe>


<h3 id="def-statements">def Statements</h3>


<ol>
  <li>Draw the function object with its intrinsic name, formal parameters, and
  parent frame. A function's parent frame is the frame in which the function was
  defined.</li>
  <li>If the intrinsic name of the function doesn't already exist in the current
  frame, write it in. If it does, erase the current binding. Bind the newly
  created function object to this name.</li>
</ol>

<iframe width="800" height="360" frameborder="0" src="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/iframe-embed(1).html">
</iframe>


<h3 id="call-expressions">Call expressions</h3>


<blockquote><p>Note: you do not have to go through this process for a built-in Python function like <code>max</code> or <code>print</code>.</p></blockquote>

<ol>
  <li>Evaluate the operator, whose value should be a function.</li>
  <li>Evaluate the operands left to right.</li>
  <li>Open a new frame. Label it with the sequential frame number, the intrinsic
  name of the function, and its parent.</li>
  <li>Bind the formal parameters of the function to the arguments whose values you
  found in step 2.</li>
  <li>Execute the body of the function in the new environment.</li>
</ol>

<iframe width="800" height="330" frameborder="0" src="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/iframe-embed(2).html">
</iframe>


<h3 id="lambdas">Lambdas</h3>


<blockquote><p><em>Note:</em> As we saw in the <code>lambda</code> expression section above, <code>lambda</code>
functions have no intrinsic name. When drawing <code>lambda</code> functions in
environment diagrams, they are labeled with the name <code>lambda</code> or with the
lowercase Greek letter λ.  This can get confusing when there are
multiple lambda functions in an environment diagram, so you can distinguish
them by numbering them or by writing the line number on which they were defined.</p></blockquote>

<ol>
  <li>Draw the lambda function object and label it with λ, its formal
  parameters, and its parent frame. A function's parent frame is the frame in
  which the function was defined.</li>
</ol>

<p>This is the only step. We are including this section to emphasize the fact that
the difference between <code>lambda</code> expressions and <code>def</code> statements is that
<code>lambda</code> expressions <em>do not</em> create any new bindings in the environment.</p>

<iframe width="800" height="330" frameborder="0" src="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/iframe-embed(3).html"> </iframe>

            <iframe width="560" height="315" src="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/IPec2A7j2bY.html" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe>
</div>


<h1 id="required-questions">Required Questions</h1>



<h2 id="what-would-python-display">What Would Python Display?</h2>


<h3 class="question" id="q1">Q1: WWPD: Lambda the Free</h3>

<blockquote><p>Use Ok to test your knowledge with the following "What Would Python Display?"
questions:</p>

<pre><code class="hljs python">python3 ok -q <span class="hljs-keyword">lambda</span> -u</code></pre>

<p>For all WWPD questions, type <code>Function</code> if you believe the answer
is <code>&lt;function...&gt;</code>, <code>Error</code> if it errors, and <code>Nothing</code> if nothing is displayed.
As a reminder, the following two lines of code will not display anything in the
Python interpreter when executed:</p>

<pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-literal">None</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>x</code></pre></blockquote>

<pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">lambda</span> x: x  <span class="hljs-comment"># A lambda expression with one parameter x</span>
<div class="alt prompt-1">______</div><div class="solution prompt-1">&lt;function &lt;<span class="hljs-keyword">lambda</span>&gt; at ...&gt;</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>a = <span class="hljs-keyword">lambda</span> x: x  <span class="hljs-comment"># Assigning the lambda function to the name a</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a(<span class="hljs-number">5</span>)
<div class="alt prompt-1">______</div><div class="solution prompt-1">5</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>(<span class="hljs-keyword">lambda</span>: <span class="hljs-number">3</span>)()  <span class="hljs-comment"># Using a lambda expression as an operator in a call exp.</span>
<div class="alt prompt-1">______</div><div class="solution prompt-1">3</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>b = <span class="hljs-keyword">lambda</span> x: <span class="hljs-keyword">lambda</span>: x  <span class="hljs-comment"># Lambdas can return other lambdas!</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>c = b(<span class="hljs-number">88</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>c
<div class="alt prompt-1">______</div><div class="solution prompt-1">&lt;function &lt;<span class="hljs-keyword">lambda</span>&gt; at ...</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>c()
<div class="alt prompt-1">______</div><div class="solution prompt-1">88</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>d = <span class="hljs-keyword">lambda</span> f: f(<span class="hljs-number">4</span>)  <span class="hljs-comment"># They can have functions as arguments as well.</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">square</span><span class="hljs-params">(x)</span>:</span>
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> x * x
<span class="hljs-meta">&gt;&gt;&gt; </span>d(square)
<div class="alt prompt-1">______</div><div class="solution prompt-1">16</div></code></pre>
        <button id="prompt-1" class="btn btn-outline btn-lg solutiontoggle prompt">
        Toggle Solution<noscript> (enable JavaScript)</noscript>
        </button>
        

<pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-literal">None</span> <span class="hljs-comment"># remember to review the rules of WWPD given above!</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>x
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">lambda</span> x: x
<div class="alt prompt-2">______</div><div class="solution prompt-2">Function</div></code></pre>
        <button id="prompt-2" class="btn btn-outline btn-lg solutiontoggle prompt">
        Toggle Solution<noscript> (enable JavaScript)</noscript>
        </button>
        

<pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>z = <span class="hljs-number">3</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>e = <span class="hljs-keyword">lambda</span> x: <span class="hljs-keyword">lambda</span> y: <span class="hljs-keyword">lambda</span>: x + y + z
<span class="hljs-meta">&gt;&gt;&gt; </span>e(<span class="hljs-number">0</span>)(<span class="hljs-number">1</span>)()
<div class="alt prompt-3">______</div><div class="solution prompt-3">4</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>f = <span class="hljs-keyword">lambda</span> z: x + z
<span class="hljs-meta">&gt;&gt;&gt; </span>f(<span class="hljs-number">3</span>)
<div class="alt prompt-3">______</div><div class="solution prompt-3">NameError: name <span class="hljs-string">'x'</span> <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> defined</div></code></pre>
        <button id="prompt-3" class="btn btn-outline btn-lg solutiontoggle prompt">
        Toggle Solution<noscript> (enable JavaScript)</noscript>
        </button>
        

<!--
<pre><code>&gt;&gt;&gt; x, y, z = 1, 2, 3
&gt;&gt;&gt; a = lambda b: x + y + z
&gt;&gt;&gt; x += y
&gt;&gt;&gt; y &#x2d;= z
&gt;&gt;&gt; a(&#x27;lambdas are fun!&#x27;)
<div class="alt prompt-4">______</div><div class="solution prompt-4">&#x35;</div></code></pre>
        <button id='prompt-4' class='btn btn-outline btn-lg solutiontoggle prompt'>
        Toggle Solution<noscript> (enable JavaScript)</noscript>
        </button>
        
-->

<pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>higher_order_lambda = <span class="hljs-keyword">lambda</span> f: <span class="hljs-keyword">lambda</span> x: f(x)
<span class="hljs-meta">&gt;&gt;&gt; </span>g = <span class="hljs-keyword">lambda</span> x: x * x
<span class="hljs-meta">&gt;&gt;&gt; </span>higher_order_lambda(<span class="hljs-number">2</span>)(g)  <span class="hljs-comment"># Which argument belongs to which function call?</span>
<div class="alt prompt-5">______</div><div class="solution prompt-5">Error</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>higher_order_lambda(g)(<span class="hljs-number">2</span>)
<div class="alt prompt-5">______</div><div class="solution prompt-5">4</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>call_thrice = <span class="hljs-keyword">lambda</span> f: <span class="hljs-keyword">lambda</span> x: f(f(f(x)))
<span class="hljs-meta">&gt;&gt;&gt; </span>call_thrice(<span class="hljs-keyword">lambda</span> y: y + <span class="hljs-number">1</span>)(<span class="hljs-number">0</span>)
<div class="alt prompt-5">______</div><div class="solution prompt-5">3</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>print_lambda = <span class="hljs-keyword">lambda</span> z: print(z)  <span class="hljs-comment"># When is the return expression of a lambda expression executed?</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>print_lambda
<div class="alt prompt-5">______</div><div class="solution prompt-5">Function</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>one_thousand = print_lambda(<span class="hljs-number">1000</span>)
<div class="alt prompt-5">______</div><div class="solution prompt-5">1000</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>one_thousand
<div class="alt prompt-5">______</div><div class="solution prompt-5"><span class="hljs-comment"># print_lambda returned None, so nothing gets displayed</span></div></code></pre>
        <button id="prompt-5" class="btn btn-outline btn-lg solutiontoggle prompt">
        Toggle Solution<noscript> (enable JavaScript)</noscript>
        </button>
        

<!--
<pre><code>&gt;&gt;&gt; bar = lambda y: lambda x: pow(x, y)
&gt;&gt;&gt; bar()(15)
<div class="alt prompt-6">______</div><div class="solution prompt-6">&#x54;&#x79;&#x70;&#x65;&#x45;&#x72;&#x72;&#x6f;&#x72;&#x3a;&#x20;&#x3c;&#x6c;&#x61;&#x6d;&#x62;&#x64;&#x61;&#x3e;&#x28;&#x29;&#x20;&#x6d;&#x69;&#x73;&#x73;&#x69;&#x6e;&#x67;&#x20;&#x31;&#x20;&#x72;&#x65;&#x71;&#x75;&#x69;&#x72;&#x65;&#x64;&#x20;&#x70;&#x6f;&#x73;&#x69;&#x74;&#x69;&#x6f;&#x6e;&#x61;&#x6c;&#x20;&#x61;&#x72;&#x67;&#x75;&#x6d;&#x65;&#x6e;&#x74;&#x3a;&#x20;&#x27;&#x79;&#x27;</div>
&gt;&gt;&gt; foo = lambda: 32
&gt;&gt;&gt; foobar = lambda x, y: x // y
&gt;&gt;&gt; a = lambda x: foobar(foo(), bar(4)(x))
&gt;&gt;&gt; a(2)
<div class="alt prompt-6">______</div><div class="solution prompt-6">&#x32;</div>
&gt;&gt;&gt; b = lambda x, y: print(&#x27;summer&#x27;)
<div class="alt prompt-6">______</div><div class="solution prompt-6">&#x23;&#x20;&#x4e;&#x6f;&#x74;&#x68;&#x69;&#x6e;&#x67;&#x20;&#x67;&#x65;&#x74;&#x73;&#x20;&#x70;&#x72;&#x69;&#x6e;&#x74;&#x65;&#x64;&#x20;&#x62;&#x79;&#x20;&#x74;&#x68;&#x65;&#x20;&#x69;&#x6e;&#x74;&#x65;&#x72;&#x70;&#x72;&#x65;&#x74;&#x65;&#x72;</div>
&gt;&gt;&gt; c = b(4, &#x27;dog&#x27;)
<div class="alt prompt-6">______</div><div class="solution prompt-6">&#x73;&#x75;&#x6d;&#x6d;&#x65;&#x72;</div>
&gt;&gt;&gt; print(c)
<div class="alt prompt-6">______</div><div class="solution prompt-6">&#x4e;&#x6f;&#x6e;&#x65;</div></code></pre>
        <button id='prompt-6' class='btn btn-outline btn-lg solutiontoggle prompt'>
        Toggle Solution<noscript> (enable JavaScript)</noscript>
        </button>
        

<pre><code>&gt;&gt;&gt; a = lambda b: b * 2
<div class="alt prompt-7">______</div><div class="solution prompt-7">&#x23;&#x20;&#x4e;&#x6f;&#x74;&#x68;&#x69;&#x6e;&#x67;&#x20;&#x67;&#x65;&#x74;&#x73;&#x20;&#x70;&#x72;&#x69;&#x6e;&#x74;&#x65;&#x64;&#x20;&#x62;&#x79;&#x20;&#x74;&#x68;&#x65;&#x20;&#x69;&#x6e;&#x74;&#x65;&#x72;&#x70;&#x72;&#x65;&#x74;&#x65;&#x72;</div>
&gt;&gt;&gt; a
<div class="alt prompt-7">______</div><div class="solution prompt-7">&#x46;&#x75;&#x6e;&#x63;&#x74;&#x69;&#x6f;&#x6e;</div>
&gt;&gt;&gt; a(a(a()))
<div class="alt prompt-7">______</div><div class="solution prompt-7">&#x54;&#x79;&#x70;&#x65;&#x45;&#x72;&#x72;&#x6f;&#x72;&#x3a;&#x20;&#x3c;&#x6c;&#x61;&#x6d;&#x62;&#x64;&#x61;&#x3e;&#x28;&#x29;&#x20;&#x6d;&#x69;&#x73;&#x73;&#x69;&#x6e;&#x67;&#x20;&#x31;&#x20;&#x72;&#x65;&#x71;&#x75;&#x69;&#x72;&#x65;&#x64;&#x20;&#x70;&#x6f;&#x73;&#x69;&#x74;&#x69;&#x6f;&#x6e;&#x61;&#x6c;&#x20;&#x61;&#x72;&#x67;&#x75;&#x6d;&#x65;&#x6e;&#x74;&#x3a;&#x20;&#x27;&#x62;&#x27;</div>
&gt;&gt;&gt; a(a(a(2)))
<div class="alt prompt-7">______</div><div class="solution prompt-7">&#x31;&#x36;</div>
&gt;&gt;&gt; def d():
...     print(None)
...     print(&#x27;whoo&#x27;)
&gt;&gt;&gt; b = d()
<div class="alt prompt-7">______</div><div class="solution prompt-7">&#x4e;&#x6f;&#x6e;&#x65;&#xa;&#x77;&#x68;&#x6f;&#x6f;</div>
&gt;&gt;&gt; b
<div class="alt prompt-7">______</div><div class="solution prompt-7">&#x23;&#x20;&#x4e;&#x6f;&#x74;&#x68;&#x69;&#x6e;&#x67;&#x20;&#x67;&#x65;&#x74;&#x73;&#x20;&#x70;&#x72;&#x69;&#x6e;&#x74;&#x65;&#x64;&#x20;&#x62;&#x79;&#x20;&#x74;&#x68;&#x65;&#x20;&#x69;&#x6e;&#x74;&#x65;&#x72;&#x70;&#x72;&#x65;&#x74;&#x65;&#x72;</div></code></pre>
        <button id='prompt-7' class='btn btn-outline btn-lg solutiontoggle prompt'>
        Toggle Solution<noscript> (enable JavaScript)</noscript>
        </button>
        
-->

<h3 class="question" id="q2">Q2: WWPD: Higher Order Functions</h3>

<blockquote><p>Use Ok to test your knowledge with the following "What Would Python Display?"
questions:</p>

<pre><code class="hljs">python3 ok -q hof-wwpd -u</code></pre>

<p>For all WWPD questions, type <code>Function</code> if you believe the answer
is <code>&lt;function...&gt;</code>, <code>Error</code> if it errors, and <code>Nothing</code> if nothing is displayed.</p></blockquote>

<pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">even</span><span class="hljs-params">(f)</span>:</span>
<span class="hljs-meta">... </span>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">odd</span><span class="hljs-params">(x)</span>:</span>
<span class="hljs-meta">... </span>        <span class="hljs-keyword">if</span> x &lt; <span class="hljs-number">0</span>:
<span class="hljs-meta">... </span>            <span class="hljs-keyword">return</span> f(-x)
<span class="hljs-meta">... </span>        <span class="hljs-keyword">return</span> f(x)
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> odd
<span class="hljs-meta">&gt;&gt;&gt; </span>steven = <span class="hljs-keyword">lambda</span> x: x
<span class="hljs-meta">&gt;&gt;&gt; </span>stewart = even(steven)
<span class="hljs-meta">&gt;&gt;&gt; </span>stewart
<div class="alt prompt-8">______</div><div class="solution prompt-8">&lt;function ...&gt;</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>stewart(<span class="hljs-number">61</span>)
<div class="alt prompt-8">______</div><div class="solution prompt-8">61</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>stewart(<span class="hljs-number">-4</span>)
<div class="alt prompt-8">______</div><div class="solution prompt-8">4</div></code></pre>
        <button id="prompt-8" class="btn btn-outline btn-lg solutiontoggle prompt">
        Toggle Solution<noscript> (enable JavaScript)</noscript>
        </button>
        

<pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">cake</span><span class="hljs-params">()</span>:</span>
<span class="hljs-meta">... </span>   print(<span class="hljs-string">'beets'</span>)
<span class="hljs-meta">... </span>   <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pie</span><span class="hljs-params">()</span>:</span>
<span class="hljs-meta">... </span>       print(<span class="hljs-string">'sweets'</span>)
<span class="hljs-meta">... </span>       <span class="hljs-keyword">return</span> <span class="hljs-string">'cake'</span>
<span class="hljs-meta">... </span>   <span class="hljs-keyword">return</span> pie
<span class="hljs-meta">&gt;&gt;&gt; </span>chocolate = cake()
<div class="alt prompt-9">______</div><div class="solution prompt-9">beets</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>chocolate
<div class="alt prompt-9">______</div><div class="solution prompt-9">Function</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>chocolate()
<div class="alt prompt-9">______</div><div class="solution prompt-9">sweets
<span class="hljs-string">'cake'</span></div>
<span class="hljs-meta">&gt;&gt;&gt; </span>more_chocolate, more_cake = chocolate(), cake
<div class="alt prompt-9">______</div><div class="solution prompt-9">sweets</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>more_chocolate
<div class="alt prompt-9">______</div><div class="solution prompt-9"><span class="hljs-string">'cake'</span></div>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">snake</span><span class="hljs-params">(x, y)</span>:</span>
<span class="hljs-meta">... </span>   <span class="hljs-keyword">if</span> cake == more_cake:
<span class="hljs-meta">... </span>       <span class="hljs-keyword">return</span> chocolate
<span class="hljs-meta">... </span>   <span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>       <span class="hljs-keyword">return</span> x + y
<span class="hljs-meta">&gt;&gt;&gt; </span>snake(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>)
<div class="alt prompt-9">______</div><div class="solution prompt-9">Function</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>snake(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>)()
<div class="alt prompt-9">______</div><div class="solution prompt-9">30</div>
<span class="hljs-meta">&gt;&gt;&gt; </span>cake = <span class="hljs-string">'cake'</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>snake(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>)
<div class="alt prompt-9">______</div><div class="solution prompt-9">30</div></code></pre>
        <button id="prompt-9" class="btn btn-outline btn-lg solutiontoggle prompt">
        Toggle Solution<noscript> (enable JavaScript)</noscript>
        </button>
        


<h2 id="coding-practice">Coding Practice</h2>


<h3 class="question" id="q3">Q3: Lambdas and Currying</h3>

<p>We can transform multiple-argument functions into a chain of
single-argument, higher order functions by taking advantage of lambda
expressions. For example, we can write a function <code>f(x, y)</code> as a different function <code>g(x)(y)</code>. This is known as <strong>currying</strong>. It's useful when dealing with functions that take only single-argument functions. We will see some examples of these later on.</p>

<p>Write a function <code>lambda_curry2</code> that will curry any two argument function using lambdas. Refer to the
<a href="http://composingprograms.com/pages/16-higher-order-functions.html#currying">textbook</a> for more details about currying.</p>

<p><strong>Your solution to this problem should fit entirely on the return line.</strong> You can try
writing it first without this restriction, but rewrite it after in one line.</p>

<solution>

<pre><code class="hljs python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">lambda_curry2</span><span class="hljs-params">(func)</span>:</span>
    <span class="hljs-string">"""
    Returns a Curried version of a two-argument function FUNC.
    &gt;&gt;&gt; from operator import add, mul, mod
    &gt;&gt;&gt; curried_add = lambda_curry2(add)
    &gt;&gt;&gt; add_three = curried_add(3)
    &gt;&gt;&gt; add_three(5)
    8
    &gt;&gt;&gt; curried_mul = lambda_curry2(mul)
    &gt;&gt;&gt; mul_5 = curried_mul(5)
    &gt;&gt;&gt; mul_5(42)
    210
    &gt;&gt;&gt; lambda_curry2(mod)(123)(10)
    3
    """</span>
    <span class="hljs-string">"*** YOUR CODE HERE ***"</span>
    <span class="hljs-keyword">return</span> ______
</code></pre>

</solution>

<p>Use Ok to test your code:</p><pre><code class="nohighlight">python3 ok -q lambda_curry2</code></pre>



<h3 class="question" id="q4">Q4: Count van Count</h3>

<p>Consider the following implementations of <code>count_factors</code> and <code>count_primes</code>:</p>

<pre><code class="hljs python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">count_factors</span><span class="hljs-params">(n)</span>:</span>
    <span class="hljs-string">"""Return the number of positive factors that n has.
    &gt;&gt;&gt; count_factors(6)
    4   # 1, 2, 3, 6
    &gt;&gt;&gt; count_factors(4)
    3   # 1, 2, 4
    """</span>
    i, count = <span class="hljs-number">1</span>, <span class="hljs-number">0</span>
    <span class="hljs-keyword">while</span> i &lt;= n:
        <span class="hljs-keyword">if</span> n % i == <span class="hljs-number">0</span>:
            count += <span class="hljs-number">1</span>
        i += <span class="hljs-number">1</span>
    <span class="hljs-keyword">return</span> count

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">count_primes</span><span class="hljs-params">(n)</span>:</span>
    <span class="hljs-string">"""Return the number of prime numbers up to and including n.
    &gt;&gt;&gt; count_primes(6)
    3   # 2, 3, 5
    &gt;&gt;&gt; count_primes(13)
    6   # 2, 3, 5, 7, 11, 13
    """</span>
    i, count = <span class="hljs-number">1</span>, <span class="hljs-number">0</span>
    <span class="hljs-keyword">while</span> i &lt;= n:
        <span class="hljs-keyword">if</span> is_prime(i):
            count += <span class="hljs-number">1</span>
        i += <span class="hljs-number">1</span>
    <span class="hljs-keyword">return</span> count

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">is_prime</span><span class="hljs-params">(n)</span>:</span>
    <span class="hljs-keyword">return</span> count_factors(n) == <span class="hljs-number">2</span> <span class="hljs-comment"># only factors are 1 and n</span></code></pre>

<p>The implementations look quite similar! Generalize this logic by writing a
function <code>count_cond</code>, which takes in a two-argument predicate function <code>condition(n,
i)</code>. <code>count_cond</code> returns a one-argument function that takes in <code>n</code>, which counts all the numbers
from 1 to <code>n</code> that satisfy <code>condition</code> when called.</p>

<solution>

<pre><code class="hljs sql">def count_cond(condition):
    """Returns a function <span class="hljs-keyword">with</span> one parameter N that counts <span class="hljs-keyword">all</span> the numbers <span class="hljs-keyword">from</span>
    <span class="hljs-number">1</span> <span class="hljs-keyword">to</span> N that satisfy the two-argument predicate <span class="hljs-keyword">function</span> Condition, <span class="hljs-keyword">where</span>
    the <span class="hljs-keyword">first</span> argument <span class="hljs-keyword">for</span> Condition <span class="hljs-keyword">is</span> N <span class="hljs-keyword">and</span> the <span class="hljs-keyword">second</span> argument <span class="hljs-keyword">is</span> the
    <span class="hljs-built_in">number</span> <span class="hljs-keyword">from</span> <span class="hljs-number">1</span> <span class="hljs-keyword">to</span> N.

    &gt;&gt;&gt; count_factors = count_cond(lambda n, i: n % i == <span class="hljs-number">0</span>)
    &gt;&gt;&gt; count_factors(<span class="hljs-number">2</span>)   <span class="hljs-comment"># 1, 2</span>
    <span class="hljs-number">2</span>
    &gt;&gt;&gt; count_factors(<span class="hljs-number">4</span>)   <span class="hljs-comment"># 1, 2, 4</span>
    <span class="hljs-number">3</span>
    &gt;&gt;&gt; count_factors(<span class="hljs-number">12</span>)  <span class="hljs-comment"># 1, 2, 3, 4, 6, 12</span>
    <span class="hljs-number">6</span>

    &gt;&gt;&gt; is_prime = lambda n, i: count_factors(i) == <span class="hljs-number">2</span>
    &gt;&gt;&gt; count_primes = count_cond(is_prime)
    &gt;&gt;&gt; count_primes(<span class="hljs-number">2</span>)    <span class="hljs-comment"># 2</span>
    <span class="hljs-number">1</span>
    &gt;&gt;&gt; count_primes(<span class="hljs-number">3</span>)    <span class="hljs-comment"># 2, 3</span>
    <span class="hljs-number">2</span>
    &gt;&gt;&gt; count_primes(<span class="hljs-number">4</span>)    <span class="hljs-comment"># 2, 3</span>
    <span class="hljs-number">2</span>
    &gt;&gt;&gt; count_primes(<span class="hljs-number">5</span>)    <span class="hljs-comment"># 2, 3, 5</span>
    <span class="hljs-number">3</span>
    &gt;&gt;&gt; count_primes(<span class="hljs-number">20</span>)   <span class="hljs-comment"># 2, 3, 5, 7, 11, 13, 17, 19</span>
    <span class="hljs-number">8</span>
    <span class="hljs-string">"""
    "</span>*** YOUR CODE HERE ***<span class="hljs-string">"
</span></code></pre>

</solution>



<p>Use Ok to test your code:</p><pre><code class="nohighlight">python3 ok -q count_cond</code></pre>


<h2 id="environment-diagram-practice">Environment Diagram Practice</h2>


<p>There is no submission for this component. However, we still encourage you to do these problems on paper to develop familiarity with Environment Diagrams, which might appear in an alternate form on the exam.</p>

<h3 class="question" id="q5">Q5: Make Adder</h3>

<p>Draw the environment diagram for the following code:</p>

<pre><code class="hljs python">n = <span class="hljs-number">9</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">make_adder</span><span class="hljs-params">(n)</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">lambda</span> k: k + n
add_ten = make_adder(n+<span class="hljs-number">1</span>)
result = add_ten(n)</code></pre>

<p>There are 3 frames total (including the Global frame). In addition, consider
the following questions:</p>

<ol>
  <li>In the Global frame, the name <code>add_ten</code> points to a function object. What is
  the intrinsic name of that function object, and what frame is its parent?</li>
  <li>What name is frame <code>f2</code> labeled with (<code>add_ten</code> or λ)?
  Which frame is the parent of <code>f2</code>?</li>
  <li>What value is the variable <code>result</code> bound to in the Global frame?</li>
</ol>

<solution>

<p>You can try out the environment diagram at <a href="http://tutor.cs61a.org/">tutor.cs61a.org</a>.
To see the environment diagram for this question, click <a href="https://goo.gl/axdNj5">here</a>.</p>

<ol>
  <li>The intrinsic name of the function object that <code>add_ten</code> points to is
  λ (specifically, the lambda whose parameter is <code>k</code>). The parent frame
  of this lambda is <code>f1</code>.</li>
  <li><code>f2</code> is labeled with the name λ the parent frame of <code>f2</code> is <code>f1</code>,
  since that is where λ is defined.</li>
  <li>The variable <code>result</code> is bound to 19.</li>
</ol>

</solution>

<h3 class="question" id="q6">Q6: Lambda the Environment Diagram</h3>

<p>Try drawing an environment diagram for the following code and predict what
Python will output.</p>

<p><strong>You do not need to submit or unlock this question through Ok.</strong>
Instead, you can check your work with the <a href="http://tutor.cs61a.org/">Online Python
Tutor</a>, but try drawing it yourself first!</p>

<pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = <span class="hljs-keyword">lambda</span> x: x * <span class="hljs-number">2</span> + <span class="hljs-number">1</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">b</span><span class="hljs-params">(b, x)</span>:</span>
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> b(x + a(x))
<span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-number">3</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>b(a, x)
<div class="alt prompt-10">______</div><div class="solution prompt-10">21 <span class="hljs-comment"># Interactive solution: https://goo.gl/Lu99QR</span></div></code></pre>
        <button id="prompt-10" class="btn btn-outline btn-lg solutiontoggle prompt">
        Toggle Solution<noscript> (enable JavaScript)</noscript>
        </button>
        


<h2 id="submit">Submit</h2>


<p>Make sure to submit this assignment by running:</p>

<pre><code class="hljs sql">python3 ok <span class="hljs-comment">--submit</span></code></pre>


<h1 id="optional-questions">Optional Questions</h1>


<h3 class="question" id="q7">Q7: Composite Identity Function</h3>

<p>Write a function that takes in two single-argument functions, <code>f</code> and <code>g</code>, and
returns another <strong>function</strong> that has a single parameter <code>x</code>. The returned
function should return <code>True</code> if <code>f(g(x))</code> is equal to <code>g(f(x))</code>. You can
assume the output of <code>g(x)</code> is a valid input for <code>f</code> and vice versa.
Try to use the <code>compose1</code> function defined below for more HOF practice.</p>

<solution>

<pre><code class="hljs python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">compose1</span><span class="hljs-params">(f, g)</span>:</span>
    <span class="hljs-string">"""Return the composition function which given x, computes f(g(x)).

    &gt;&gt;&gt; add_one = lambda x: x + 1        # adds one to x
    &gt;&gt;&gt; square = lambda x: x**2
    &gt;&gt;&gt; a1 = compose1(square, add_one)   # (x + 1)^2
    &gt;&gt;&gt; a1(4)
    25
    &gt;&gt;&gt; mul_three = lambda x: x * 3      # multiplies 3 to x
    &gt;&gt;&gt; a2 = compose1(mul_three, a1)    # ((x + 1)^2) * 3
    &gt;&gt;&gt; a2(4)
    75
    &gt;&gt;&gt; a2(5)
    108
    """</span>
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">lambda</span> x: f(g(x))

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">composite_identity</span><span class="hljs-params">(f, g)</span>:</span>
    <span class="hljs-string">"""
    Return a function with one parameter x that returns True if f(g(x)) is
    equal to g(f(x)). You can assume the result of g(x) is a valid input for f
    and vice versa.

    &gt;&gt;&gt; add_one = lambda x: x + 1        # adds one to x
    &gt;&gt;&gt; square = lambda x: x**2
    &gt;&gt;&gt; b1 = composite_identity(square, add_one)
    &gt;&gt;&gt; b1(0)                            # (0 + 1)^2 == 0^2 + 1
    True
    &gt;&gt;&gt; b1(4)                            # (4 + 1)^2 != 4^2 + 1
    False
    """</span>
    <span class="hljs-string">"*** YOUR CODE HERE ***"</span>
</code></pre>

</solution>



<p>Use Ok to test your code:</p><pre><code class="nohighlight">python3 ok -q composite_identity</code></pre>

<h3 class="question" id="q8">Q8: I Heard You Liked Functions...</h3>

<p>Define a function <code>cycle</code> that takes in three functions <code>f1</code>, <code>f2</code>,
<code>f3</code>, as arguments. <code>cycle</code> will return another function that should
take in an integer argument <code>n</code> and return another function.  That
final function should take in an argument <code>x</code> and cycle through
applying <code>f1</code>, <code>f2</code>, and <code>f3</code> to <code>x</code>, depending on what <code>n</code>
was. Here's what the final function should do to <code>x</code> for a few
values of <code>n</code>:</p>

<ul>
  <li><code>n = 0</code>, return <code>x</code></li>
  <li><code>n = 1</code>, apply <code>f1</code> to <code>x</code>, or return <code>f1(x)</code></li>
  <li><code>n = 2</code>, apply <code>f1</code> to <code>x</code> and then <code>f2</code> to the result of that, or
  return <code>f2(f1(x))</code></li>
  <li><code>n = 3</code>, apply <code>f1</code> to <code>x</code>, <code>f2</code> to the result of applying <code>f1</code>,
  and then <code>f3</code> to the result of applying <code>f2</code>, or <code>f3(f2(f1(x)))</code></li>
  <li><code>n = 4</code>, start the cycle again applying <code>f1</code>, then <code>f2</code>, then <code>f3</code>,
  then <code>f1</code> again, or <code>f1(f3(f2(f1(x))))</code></li>
  <li>And so forth.</li>
</ul>

<p><em>Hint</em>: most of the work goes inside the most nested function.</p>

<solution>

<pre><code class="hljs python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">cycle</span><span class="hljs-params">(f1, f2, f3)</span>:</span>
    <span class="hljs-string">"""Returns a function that is itself a higher-order function.

    &gt;&gt;&gt; def add1(x):
    ...     return x + 1
    &gt;&gt;&gt; def times2(x):
    ...     return x * 2
    &gt;&gt;&gt; def add3(x):
    ...     return x + 3
    &gt;&gt;&gt; my_cycle = cycle(add1, times2, add3)
    &gt;&gt;&gt; identity = my_cycle(0)
    &gt;&gt;&gt; identity(5)
    5
    &gt;&gt;&gt; add_one_then_double = my_cycle(2)
    &gt;&gt;&gt; add_one_then_double(1)
    4
    &gt;&gt;&gt; do_all_functions = my_cycle(3)
    &gt;&gt;&gt; do_all_functions(2)
    9
    &gt;&gt;&gt; do_more_than_a_cycle = my_cycle(4)
    &gt;&gt;&gt; do_more_than_a_cycle(2)
    10
    &gt;&gt;&gt; do_two_cycles = my_cycle(6)
    &gt;&gt;&gt; do_two_cycles(1)
    19
    """</span>
    <span class="hljs-string">"*** YOUR CODE HERE ***"</span>
</code></pre>

</solution>



<p>Use Ok to test your code:</p><pre><code class="nohighlight">python3 ok -q cycle</code></pre>
<script>
    $('.alwaystoggle').css('display', 'inline-block');
    $('.alwaystoggle').click(function() {
      var solution_id = $(this).attr('id');
      $('div.' + solution_id).slideToggle(600);
    });
</script>

  </div>

  <div class="col-md-3 sticky">
    <nav class="hidden-print hidden-sm hidden-xs sidebar">
      <ul class="nav nav-stacked noselect">
  <li class=""><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#topics">Topics</a></li>
  <ul class="nav nav-stacked noselect">
    <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#lambda-expressions">Lambda Expressions</a></li>
    <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#environment-diagrams">Environment Diagrams</a></li>
    <ul class="nav nav-stacked noselect">
      <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#assignment-statements">Assignment Statements</a></li>
      <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#def-statements">def Statements</a></li>
      <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#call-expressions">Call expressions</a></li>
      <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#lambdas">Lambdas</a></li>
    </ul>
  </ul>
  <li class=""><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#required-questions">Required Questions</a></li>
  <ul class="nav nav-stacked noselect">
    <li class=""><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#what-would-python-display">What Would Python Display?</a></li>
    <ul class="nav nav-stacked noselect">
      <li class=""><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#q1">Q1: WWPD: Lambda the Free</a></li>
      <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#q2">Q2: WWPD: Higher Order Functions</a></li>
    </ul>
    <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#coding-practice">Coding Practice</a></li>
    <ul class="nav nav-stacked noselect">
      <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#q3">Q3: Lambdas and Currying</a></li>
      <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#q4">Q4: Count van Count</a></li>
    </ul>
    <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#environment-diagram-practice">Environment Diagram Practice</a></li>
    <ul class="nav nav-stacked noselect">
      <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#q5">Q5: Make Adder</a></li>
      <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#q6">Q6: Lambda the Environment Diagram</a></li>
    </ul>
    <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#submit">Submit</a></li>
  </ul>
  <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#optional-questions">Optional Questions</a></li>
  <ul class="nav nav-stacked noselect">
    <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#q7">Q7: Composite Identity Function</a></li>
    <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/lab/lab02/#q8">Q8: I Heard You Liked Functions...</a></li>
  </ul>
</ul>
    </nav>
  </div>
</div>

    </main>

    <footer class="container">
      <div class="row text-center">
        <div class="col col-sm-4">
          <h3><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/">CS 61A</a></h3>
          <ul class="nav nav-pills nav-stacked">
            <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/weekly.html">Weekly Schedule</a></li>
            <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/office-hours.html">Office Hours</a></li>
            <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/staff.html">Staff</a></li>
          </ul>
        </div>
        <div class="col col-sm-4">
          <h3><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/resources.html">Resources</a></h3>
          <ul class="nav nav-pills nav-stacked">
            <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/articles/studying.html">Studying Guide</a></li>
            <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/articles/debugging.html">Debugging Guide</a></li>
            <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/articles/composition.html">Composition Guide</a></li>
          </ul>
        </div>
        <div class="col col-sm-4">
          <h3><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/articles/about.html">Policies</a></h3>
          <ul class="nav nav-pills nav-stacked">
            <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/articles/about.html#assignments">Assignments</a></li>
            <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/articles/about.html#exams">Exams</a></li>
            <li><a href="https://inst.eecs.berkeley.edu/~cs61a/fa20/articles/about.html#grading">Grading</a></li>
          </ul>
        </div>
      </div>
    </footer>

    

<script src="./Lab 2_ Higher-Order Functions, Lambda Expressions _ CS 61A Fall 2020_files/sketchy.js.下载"></script>
<script>
  $('.sidebar ul').addClass('nav nav-stacked noselect');
  $('body').scrollspy({
    target: '.sidebar',
    offset: 40
  });

  function goToId(id) {
    var target = $(id);
    target.parent().show();
    $('html,body').animate({
      scrollTop: target.offset().top,
    }, 100);
    $("body").scrollspy('refresh');
  }

  if (location.hash) {
    setTimeout(function() {
      if (location.hash) {
        goToId(location.hash);
      }
    }, 1);
  }

  $("a").click(function(event) {
    var urlBeforeHashRegEx = new RegExp("^"+window.location.href.split("#")[0]);
    if (/^#/.test(this.hash) && urlBeforeHashRegEx.test(this.href)) {
      event.preventDefault();
      goToId(this.hash);
      document.location.hash = this.hash;
    }
  });
</script>

  
<canvas width="1528" height="10471" style="cursor: crosshair; top: 0px; left: 0px; position: absolute; z-index: 99; background-color: rgba(0, 0, 0, 0.25); user-select: none; pointer-events: none; display: none;"></canvas></body><div style="all: initial;"><div id="__hcfy__" style="all: initial;"><template shadowrootmode="open"><style>#root{-webkit-text-size-adjust:100%;box-sizing:border-box;font-size:14px;font-weight:400;letter-spacing:0;line-height:1.28581;text-transform:none;color:#182026;font-family:-apple-system,"BlinkMacSystemFont","Segoe UI","Roboto","Oxygen","Ubuntu","Cantarell","Open Sans","Helvetica Neue","Icons16",sans-serif}#root{touch-action:manipulation}#root>.bp5-portal{z-index:9999999999}</style><link rel="stylesheet" href="chrome-extension://oikmahiipjniocckomdccmplodldodja/normalize.css"><link rel="stylesheet" href="chrome-extension://oikmahiipjniocckomdccmplodldodja/blueprint.css"><link rel="stylesheet" href="chrome-extension://oikmahiipjniocckomdccmplodldodja/blueprint-select.css"><link rel="stylesheet" href="chrome-extension://oikmahiipjniocckomdccmplodldodja/cropper.css"><style>#translate-panel{background-color:#f6f7f9;display:flex;flex-direction:column;padding-bottom:8px}.bp5-dark #translate-panel{background-color:#252a31}#translate-panel .fixed{flex-shrink:0;margin-bottom:10px}#translate-panel .body{flex-grow:1;overflow:auto;overscroll-behavior:contain}#translate-panel .body::-webkit-scrollbar{width:8px;background-color:rgba(0,0,0,0);-webkit-border-radius:100px}#translate-panel .body::-webkit-scrollbar:hover{background-color:rgba(0,0,0,.09)}#translate-panel .body::-webkit-scrollbar-thumb:vertical{background:rgba(0,0,0,.5);-webkit-border-radius:100px}#translate-panel .body::-webkit-scrollbar-thumb:vertical:active{background:rgba(0,0,0,.61);-webkit-border-radius:100px}#translate-panel.size-small,#translate-panel.size-small h6.bp5-heading,#translate-panel.size-small .bp5-control.bp5-large,#translate-panel.size-small textarea.bp5-input.bp5-small{font-size:14px}#translate-panel.size-small .phonetic-item,#translate-panel.size-small .quick-settings a{font-size:12px}#translate-panel.size-middle,#translate-panel.size-middle h6.bp5-heading,#translate-panel.size-middle .bp5-control.bp5-large,#translate-panel.size-middle textarea.bp5-input{font-size:18px}#translate-panel.size-middle .phonetic-item,#translate-panel.size-middle .quick-settings a{font-size:14px}#translate-panel.size-large,#translate-panel.size-large h6.bp5-heading,#translate-panel.size-large .bp5-control.bp5-large,#translate-panel.size-large textarea.bp5-input.bp5-large{font-size:22px}#translate-panel.size-large .source,#translate-panel.size-large .phonetic-item,#translate-panel.size-large .quick-settings a{font-size:18px}#translate-panel .bp5-button.bp5-small,#translate-panel .bp5-small .bp5-button{min-height:20px;min-width:20px}#translate-panel .header{display:flex;align-items:center;padding:4px 6px 4px 10px;border-bottom:1px solid #d1d1d1}.bp5-dark #translate-panel .header{border-bottom-color:rgba(17,20,24,.4)}#translate-panel .header .drag-block{min-width:5px;flex-shrink:0;flex-grow:1;align-self:stretch}#translate-panel .header .left{flex-shrink:0;display:flex}#translate-panel .header .right{flex-shrink:0;display:flex;align-items:center}#translate-panel .header .right .bp5-icon-arrow-right{flex-shrink:0;margin:0 5px}#translate-panel .header .right>.bp5-button{flex-shrink:0;margin:0 1px}#translate-panel .header .right>.bp5-button:last-child{margin-right:0}#translate-panel .quick-settings{padding:4px 9px;margin:0 1px}#translate-panel .quick-settings>div{margin-bottom:5px}#translate-panel .quick-settings .bp5-control{margin-bottom:0}#translate-panel .query-text{position:relative;padding:10px 10px 2px 10px}#translate-panel .query-text textarea.bp5-input{min-height:44px;font-family:system-ui,-apple-system,"Segoe UI","Roboto","Ubuntu","Cantarell","Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";overscroll-behavior:contain}#translate-panel .query-text .translate-btn{position:absolute;opacity:.6}#translate-panel .query-text .translate-btn:hover{opacity:1}#translate-panel .body{padding:0 10px}#translate-panel .body .bp5-card:first-child{margin-top:1px}#translate-panel .body .bp5-card:last-child{margin-bottom:1px}#translate-panel .body .no-api{margin:20px 0}.result-block{margin:8px 0;padding:2px 5px}.result-block .bp5-button{visibility:hidden}.result-block .error .bp5-button,.result-block:hover .bp5-button{visibility:visible}.result-block .legend{display:flex;align-items:center;justify-content:space-between}.result-block .legend .legend-left{display:flex;align-items:center}.result-block .legend .api-ico,.result-block .legend .bp5-heading{flex-shrink:0;white-space:nowrap}.result-block .legend .api-ico{display:inline-block;width:14px;height:14px;background-size:contain;margin-right:3px}.result-block .legend .bp5-heading{margin-bottom:0;margin-right:10px}.result-block .legend .source{cursor:pointer;font-size:12px;display:inline-flex;align-items:center}.result-block .legend .source .source-text{overflow:hidden}.result-block .legend .source .bp5-icon{position:relative;top:-1px;margin-left:1px}.result-block .phonetic{display:flex;flex-wrap:wrap}.result-block .phonetic .phonetic-item{display:flex;align-items:center;font-size:12px}.result-block .phonetic .phonetic-item:not(:last-child){margin-right:12px}.result-block .common-result p{line-height:1.3;margin:2px 0;overflow-wrap:break-word}.result-block .common-result .dict a{text-decoration:underline}.result-block .error{font-size:12px;padding:5px 10px}.result-block .dict-pos{margin-right:5px}.external-translators{margin-bottom:3px;padding:0;display:flex;flex-wrap:wrap}.external-translators>div{margin:0 5px 5px 0}.quick-links a{margin:0 5px 5px 0}#popper-container{width:250px;max-width:100%;position:absolute;left:0;top:0;z-index:9999999998;touch-action:none;transition:opacity .2s;background-color:#f6f7f9}.bp5-dark #popper-container{background-color:#252a31}#popper-container.show{opacity:1;pointer-events:auto;user-select:auto}#popper-container,#popper-container[data-popper-reference-hidden=true]{opacity:0;pointer-events:none;user-select:none}#popper-container .drag-block{cursor:grab}#popper-container.pin{position:fixed}#popper-container.pin .arrow{display:none}#popper-container .arrow,#popper-container .arrow::before{position:absolute;width:8px;height:8px;z-index:-1}#popper-container .arrow::before{content:"";transform:rotate(45deg);background:#f6f7f9}.bp5-dark #popper-container .arrow::before{background-color:#252a31}#popper-container .arrow{display:none}#popper-container.show[data-popper-placement]:not([data-popper-reference-hidden=true]) .arrow{display:block}#popper-container[data-popper-placement^=top] .arrow{bottom:-5px}#popper-container[data-popper-placement^=top] .arrow::before{border-right:1px solid #d1d1d1;border-bottom:1px solid #d1d1d1}#popper-container[data-popper-placement^=bottom] .arrow{top:-5px}#popper-container[data-popper-placement^=bottom] .arrow::before{border-left:1px solid #d1d1d1;border-top:1px solid #d1d1d1}#popper-container[data-popper-placement^=left] .arrow{right:-5px}#popper-container[data-popper-placement^=left] .arrow::before{border-right:1px solid #d1d1d1;border-top:1px solid #d1d1d1}#popper-container[data-popper-placement^=right] .arrow{left:-5px}#popper-container[data-popper-placement^=right] .arrow::before{border-left:1px solid #d1d1d1;border-bottom:1px solid #d1d1d1}#translate-btn{display:none;position:absolute;z-index:9999999999;left:0;top:0}#translate-btn .bp5-button{padding:2px;min-width:0;min-height:0}#translate-btn .btn-icon{width:18px;height:18px;background-image:url(chrome-extension://oikmahiipjniocckomdccmplodldodja/logo.png);background-size:contain}.bp5-dark #translate-btn .btn-icon{background-image:url(chrome-extension://oikmahiipjniocckomdccmplodldodja/logowhite36.png)}#translate-btn.show{display:block}.translate-type-selector .bp5-label{display:inline}.translate-type-selector .bp5-radio{margin-bottom:0}#ocr-container{position:fixed;z-index:99999999999999;left:0;top:0;right:0;bottom:0}#ocr-container .toolbar{display:none;position:absolute;z-index:1}#ocr-container img{max-width:100%}#app{cursor:default}.switch-pin{flex-shrink:0;cursor:pointer}.switch-pin .bp5-icon-pin{transition:transform .2s,color .2s;transform:rotate(-45deg)}.pin .switch-pin .bp5-icon-pin{transform:rotate(-70deg)}.cut-btn{margin-left:2px}.app-toaster-container{position:fixed !important;z-index:9999999999 !important}.app-toaster-container .bp5-toast{min-width:auto}#web-trs-panel .app-toaster-container{padding-right:0;padding-left:0}#web-trs-panel .page-trs-form-group{margin:0 0 0 0;align-items:center}#web-trs-panel .page-trs-form-group>label{width:70px}</style><div id="root" dir="ltr" class="bp5-dark"><div id="app" class="bp5-dark"><div id="translate-btn"><button type="button" class="bp5-button"><span class="btn-icon"></span></button></div><div id="popper-container" class="bp5-elevation-4" style="width: 290px; transform: translate(0px, 0px);"><div id="translate-panel" class="size-small"><div class="fixed"><div class="header"><div class="left"><div class="switch-pin"><button type="button" class="bp5-button bp5-minimal bp5-small"><span aria-hidden="true" class="bp5-icon bp5-icon-pin"><svg data-icon="pin" height="14" role="img" viewBox="0 0 16 16" width="14"><path d="M9.41.92c-.51.51-.41 1.5.15 2.56L4.34 7.54C2.8 6.48 1.45 6.05.92 6.58l3.54 3.54-3.54 4.95 4.95-3.54 3.54 3.54c.53-.53.1-1.88-.96-3.42l4.06-5.22c1.06.56 2.04.66 2.55.15L9.41.92z" fill-rule="evenodd"></path></svg></span></button></div><button type="button" title="图片翻译" class="bp5-button bp5-minimal bp5-small"><span aria-hidden="true" class="bp5-icon bp5-icon-media"><svg data-icon="media" height="14" role="img" viewBox="0 0 16 16" width="14"><path d="M11.99 6.99c.55 0 1-.45 1-1s-.45-1-1-1-1 .45-1 1 .45 1 1 1zm3-5h-14c-.55 0-1 .45-1 1v10c0 .55.45 1 1 1h14c.55 0 1-.45 1-1v-10c0-.55-.45-1-1-1zm-1 9l-5-3-1 2-3-4-3 5v-7h12v7z" fill-rule="evenodd"></path></svg></span></button><button type="button" title="语音翻译" class="bp5-button bp5-minimal bp5-small"><span class="bp5-icon"><svg version="1.1" id="Capa_1" width="14" height="14" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 490.9 490.9" xml:space="preserve"><g><g><path d="M245.5,322.9c53,0,96.2-43.2,96.2-96.2V96.2c0-53-43.2-96.2-96.2-96.2s-96.2,43.2-96.2,96.2v130.5 C149.3,279.8,192.5,322.9,245.5,322.9z M173.8,96.2c0-39.5,32.2-71.7,71.7-71.7s71.7,32.2,71.7,71.7v130.5 c0,39.5-32.2,71.7-71.7,71.7s-71.7-32.2-71.7-71.7V96.2z"></path><path d="M94.4,214.5c-6.8,0-12.3,5.5-12.3,12.3c0,85.9,66.7,156.6,151.1,162.8v76.7h-63.9c-6.8,0-12.3,5.5-12.3,12.3 s5.5,12.3,12.3,12.3h152.3c6.8,0,12.3-5.5,12.3-12.3s-5.5-12.3-12.3-12.3h-63.9v-76.7c84.4-6.3,151.1-76.9,151.1-162.8 c0-6.8-5.5-12.3-12.3-12.3s-12.3,5.5-12.3,12.3c0,76.6-62.3,138.9-138.9,138.9s-138.9-62.3-138.9-138.9 C106.6,220,101.2,214.5,94.4,214.5z"></path></g></g></svg></span></button></div><div class="drag-block" title="按住不放可以拖动"></div><div class="right"><div style="font-size: 0px; position: relative;"><button type="button" title="你有 2 条未读消息" class="bp5-button bp5-minimal bp5-small"><span aria-hidden="true" class="bp5-icon bp5-icon-notifications"><svg data-icon="notifications" height="14" role="img" viewBox="0 0 16 16" width="14"><path d="M8 16c1.1 0 2-.9 2-2H6c0 1.1.9 2 2 2zm6-5c-.55 0-1-.45-1-1V6c0-2.43-1.73-4.45-4.02-4.9 0-.04.02-.06.02-.1 0-.55-.45-1-1-1S7 .45 7 1c0 .04.02.06.02.1A4.992 4.992 0 003 6v4c0 .55-.45 1-1 1s-1 .45-1 1 .45 1 1 1h12c.55 0 1-.45 1-1s-.45-1-1-1z" fill-rule="evenodd"></path></svg></span></button><div style="position: absolute; background: rgb(243, 1, 1); width: 6px; height: 6px; border-radius: 3px; right: 3px; top: 1px;"></div></div><button type="button" disabled="" title="添加到收藏夹" class="bp5-button bp5-disabled bp5-minimal bp5-small" tabindex="-1"><span aria-hidden="true" class="bp5-icon bp5-icon-star-empty"><svg data-icon="star-empty" height="14" role="img" viewBox="0 0 16 16" width="14"><path d="M16 6.11l-5.53-.84L8 0 5.53 5.27 0 6.11l4 4.1L3.06 16 8 13.27 12.94 16 12 10.21l4-4.1zM4.91 13.2l.59-3.62L3 7.02l3.45-.53L8 3.2l1.55 3.29 3.45.53-2.5 2.56.59 3.62L8 11.49 4.91 13.2z" fill-rule="evenodd"></path></svg></span></button><button type="button" class="bp5-button bp5-minimal bp5-small settings" title="快捷设置"><span aria-hidden="true" class="bp5-icon bp5-icon-cog"><svg data-icon="cog" height="14" role="img" viewBox="0 0 16 16" width="14"><path d="M15.19 6.39h-1.85c-.11-.37-.27-.71-.45-1.04l1.36-1.36c.31-.31.31-.82 0-1.13l-1.13-1.13a.803.803 0 00-1.13 0l-1.36 1.36c-.33-.17-.67-.33-1.04-.44V.79c0-.44-.36-.8-.8-.8h-1.6c-.44 0-.8.36-.8.8v1.86c-.39.12-.75.28-1.1.47l-1.3-1.3c-.3-.3-.79-.3-1.09 0L1.82 2.91c-.3.3-.3.79 0 1.09l1.3 1.3c-.2.34-.36.7-.48 1.09H.79c-.44 0-.8.36-.8.8v1.6c0 .44.36.8.8.8h1.85c.11.37.27.71.45 1.04l-1.36 1.36c-.31.31-.31.82 0 1.13l1.13 1.13c.31.31.82.31 1.13 0l1.36-1.36c.33.18.67.33 1.04.44v1.86c0 .44.36.8.8.8h1.6c.44 0 .8-.36.8-.8v-1.86c.39-.12.75-.28 1.1-.47l1.3 1.3c.3.3.79.3 1.09 0l1.09-1.09c.3-.3.3-.79 0-1.09l-1.3-1.3c.19-.35.36-.71.48-1.1h1.85c.44 0 .8-.36.8-.8v-1.6a.816.816 0 00-.81-.79zm-7.2 4.6c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3z" fill-rule="evenodd"></path></svg></span></button><button type="button" title="关闭(Esc)" class="bp5-button bp5-minimal bp5-small"><span aria-hidden="true" class="bp5-icon bp5-icon-cross"><svg data-icon="cross" height="18" role="img" viewBox="0 0 16 16" width="18"><path d="M9.41 8l3.29-3.29c.19-.18.3-.43.3-.71a1.003 1.003 0 00-1.71-.71L8 6.59l-3.29-3.3a1.003 1.003 0 00-1.42 1.42L6.59 8 3.3 11.29c-.19.18-.3.43-.3.71a1.003 1.003 0 001.71.71L8 9.41l3.29 3.29c.18.19.43.3.71.3a1.003 1.003 0 00.71-1.71L9.41 8z" fill-rule="evenodd"></path></svg></span></button></div></div><div class="bp5-collapse"><div class="bp5-collapse-body" aria-hidden="true"><div class="quick-settings bp5-card bp5-elevation-0"><div><span aria-controls="listbox-0" aria-haspopup="listbox" class="lang-select bp5-popover-target" aria-expanded="false" role="combobox"><button type="button" class="bp5-button bp5-small"><span class="bp5-button-text">自动检测</span><span aria-hidden="true" class="bp5-icon bp5-icon-caret-down"><svg data-icon="caret-down" height="16" role="img" viewBox="0 0 16 16" width="16"><path d="M12 6.5c0-.28-.22-.5-.5-.5h-7a.495.495 0 00-.37.83l3.5 4c.09.1.22.17.37.17s.28-.07.37-.17l3.5-4c.08-.09.13-.2.13-.33z" fill-rule="evenodd"></path></svg></span></button></span><span aria-hidden="true" class="bp5-icon bp5-icon-arrow-right" style="margin: 0px 10px;"><svg data-icon="arrow-right" height="12" role="img" viewBox="0 0 16 16" width="12"><path d="M14.7 7.29l-5-5a.965.965 0 00-.71-.3 1.003 1.003 0 00-.71 1.71l3.29 3.29H1.99c-.55 0-1 .45-1 1s.45 1 1 1h9.59l-3.29 3.29a1.003 1.003 0 001.42 1.42l5-5c.18-.18.29-.43.29-.71s-.12-.52-.3-.7z" fill-rule="evenodd"></path></svg></span><span aria-controls="listbox-1" aria-haspopup="listbox" class="lang-select bp5-popover-target" aria-expanded="false" role="combobox"><button type="button" class="bp5-button bp5-small"><span class="bp5-button-text">中文(简体)</span><span aria-hidden="true" class="bp5-icon bp5-icon-caret-down"><svg data-icon="caret-down" height="16" role="img" viewBox="0 0 16 16" width="16"><path d="M12 6.5c0-.28-.22-.5-.5-.5h-7a.495.495 0 00-.37.83l3.5 4c.09.1.22.17.37.17s.28-.07.37-.17l3.5-4c.08-.09.13-.2.13-.33z" fill-rule="evenodd"></path></svg></span></button></span></div><div><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>谷歌</label><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>DeepL</label><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>ChatGPT</label><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>Yandex</label><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>百度</label><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox" checked=""><span class="bp5-control-indicator"></span>百度(专业版)</label><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>腾讯</label><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>彩云</label><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>阿里</label><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>阿里(专业版)</label><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>有道</label><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>火山</label><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox" checked=""><span class="bp5-control-indicator"></span>必应词典</label><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>搜狗</label></div><div><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>自动朗读</label><span aria-haspopup="true" class="bp5-popover-target"><label class="bp5-control bp5-checkbox bp5-disabled bp5-inline"><input disabled="" tabindex="0" type="checkbox"><span class="bp5-control-indicator"></span>自动复制</label></span></div><div><label class="bp5-control bp5-radio bp5-inline"><input name="Blueprint5.RadioGroup-0" type="radio" value="small" checked=""><span class="bp5-control-indicator"></span>小</label><label class="bp5-control bp5-radio bp5-inline"><input name="Blueprint5.RadioGroup-0" type="radio" value="middle"><span class="bp5-control-indicator"></span>中</label><label class="bp5-control bp5-radio bp5-inline"><input name="Blueprint5.RadioGroup-0" type="radio" value="large"><span class="bp5-control-indicator"></span>大</label></div><div><label class="bp5-control bp5-checkbox bp5-inline"><input type="checkbox"><span class="bp5-control-indicator"></span>显示文本框</label><label class="bp5-control bp5-checkbox bp5-inline" style="margin-right: 0px;"><input type="checkbox"><span class="bp5-control-indicator"></span>鼠标悬浮取词</label></div><a class="bp5-text-small">打开完整设置</a><a class="bp5-text-small" style="margin-left: 15px;"><span aria-hidden="true" class="bp5-icon bp5-icon-crown" style="position: relative; top: -1px;"><svg data-icon="crown" height="14" role="img" viewBox="0 0 16 16" width="14"><path d="M2 6l3 2 3-4 3 4 3-2-1 6H3L2 6zm6-5a1 1 0 110 2 1 1 0 010-2zM1 3a1 1 0 110 2 1 1 0 010-2zm14 0a1 1 0 110 2 1 1 0 010-2zM3 13h10v2H3v-2z" fill-rule="evenodd"></path></svg></span> 开通会员</a></div></div></div></div><div class="body"><p>请输入需要翻译的文本。</p></div></div><div class="arrow"></div></div><div id="web-trs-panel"></div></div></div></template></div></div></html>